import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_soundmagus/extension/text_editing_controller.dart';
import 'package:flutter_soundmagus/main.dart';
import 'package:flutter_soundmagus/util/ble_helper.dart';
import 'package:flutter_soundmagus/widget/eq/eq.dart';
import 'package:get/get.dart';

import '../../common/global.dart';
part 'delay_item_controller.dart';

class DelayController extends GetxController with MsgParserRefreshMixin {
  ///单位
  final unit = UnitType.ms.obs;

  ///选中通道索引
  final selectCH = 0.obs;

  ///总音量
  final volume = 0.0.obs;

  ///总音量是否静音
  final mute = false.obs;

  ///首次点link的通道（需要作为被联动的通道）
  final linkedFirst = RxnInt(null);

  late List<DelayItemController> delayItemControllers;

  AdvancedModel get model => DataMng().advancedModel;

  DelayController() {
    delayItemControllers = List.generate(kChCount, (index) {
      var model = DataMng().advancedModel;
      //总音量
      volume.value = model.level.totalModel.volume.toDouble();

      var level = model.level.items[index];
      var timeDelay = model.timeDelay;
      var c = DelayItemController(this)
        ..index = index
        ..delay.value = timeDelay.timeDelays[index]
        ..phase.value = level.paraphase
        ..mute.value = level.mute
        ..volume.value = level.volume.toDouble();
      return c;
    });

    observeRefresh();

    refreshData(false);

    ever<UnitType>(unit, (v) {
      delayItemControllers.forEach((e) {
        e.refreshDelayText();
      });
    });

    ever<double>(volume, (v) {
      model.level.totalModel.volume = v.toInt();
    });

    debounce<double>(volume, (v) {
      print('volume->$v');
      final rs = (-v * kVolumeRadio).toInt();
      Msg.msgVolumeTotal(rs).send();
    }, time: kMsgInterval);

    ever<bool>(mute, (v) {
      model.level.totalModel.mute = v;
    });

    debounce<bool>(mute, (v) {
      Msg.msgVolumeTotal(volume.value.toInt(), mute: v).send();
    }, time: kMsgInterval);
  }
  @override
  refreshData(bool needSendData) {
    super.refreshData(needSendData);

    for (var index = 0; index < delayItemControllers.length; index++) {
      var model = DataMng().advancedModel;
      //总音量
      volume.value = model.level.totalModel.volume.toDouble();

      var level = model.level.items[index];
      var timeDelay = model.timeDelay;
      delayItemControllers[index]
        ..index = index
        ..delay.value = timeDelay.timeDelays[index]
        ..phase.value = level.paraphase
        ..mute.value = level.mute
        ..volume.value = level.volume.toDouble();
    }
  }

  //刷新UI
  @override
  refreshState(bool needSendData) {
    super.refreshState(needSendData);
  }

  void setUnit(int index) {
    unit.value = UnitType.values[index];
  }

  void nextUnit() {
    var i = UnitType.values.indexOf(unit.value);
    i++;
    if (i >= UnitType.values.length) {
      i = 0;
    }
    unit.value = UnitType.values[i];
  }

  void setChannel(int index) {
    selectCH.value = index;
  }
}
